﻿using System;
using System.Web.UI;
using ZX.CRM.Bussiness;
using System.Data;

namespace ZX.WebApp.Crm.Report
{
    public partial class RptValueSale : System.Web.UI.Page
    {
        BCrmValue bCrmValue = new BCrmValue();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStime.Value = DateTime.Now.Year.ToString() + "-01-01";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
            }
            if (txtStime.Value != "")
            {
                if (txtEtime.Value == "")
                {
                    txtEtime.Value = DateTime.Now.ToString();
                }
                DoReport();
            }
            else
            {
                DoReport();
            }
        }
        private void DoReport()
        {
            bind(txtStime.Value, txtEtime.Value);
            getCss();
        }

        private void bind(string sDate, string eDate)
        {
            try
            {
                DataSet myDataSet = new DataSet();
                myDataSet.DataSetName = "DsValueAccount";
                DataTable dtValue = bCrmValue.ReturnDsValue(sDate, eDate, "sp_CrmValueSaleRoad");
                DataTable dtProduct = bCrmValue.ReturnDsValue(sDate, eDate, "sp_CrmValueProduct");
                dtValue.TableName = "dtValue";
                dtProduct.TableName = "dtProduct";
                if(dtValue!=null)
                myDataSet.Tables.Add(dtValue.Copy());
                decimal totalMoney = 0;
                if (dtValue != null && dtValue.Rows.Count > 0)
                {
                    for (int i = 0; i < dtValue.Rows.Count; i++)
                    {
                        totalMoney = totalMoney + (decimal)dtValue.Rows[i][1];
                    }
                }
                lblTotalMoney.Text = totalMoney.ToString();
                if (dtProduct != null) myDataSet.Tables.Add(dtProduct.Copy());
                ZX.CRM.Report.CrmReport.RptValueSale crm = new ZX.CRM.Report.CrmReport.RptValueSale();
                crm.DataSource = myDataSet;
                RV.Report = crm;
                
            }
            catch
            {
            }
        }
        private void getStartEndDate()
        {
            if (hdnFd.Value == "month")
            {
                txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
            }
            else if (hdnFd.Value == "year")
            {
                txtStime.Value = DateTime.Now.Year.ToString() + "-01-01";
                txtEtime.Value = DateTime.Now.Year.ToString() + "-12-31";
            }
            else if (hdnFd.Value == "season")
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 1)
                {
                    txtStime.Value = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); txtEtime.Value = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (season == 2)
                {
                    txtStime.Value = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01";
                    txtEtime.Value = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
            }
        }
        private void getCss()
        {
            lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
            lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
            lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
            if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "red");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
            }
            else if (txtStime.Value == DateTime.Now.Year.ToString() + "-01-01" &&
                txtEtime.Value == DateTime.Now.Year.ToString() + "-12-31")
            {
                lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "blue");
                lnkYear.Style.Add(HtmlTextWriterStyle.Color, "red");
            }
            else
            {
                int season = DateTime.Now.Month % 3;
                if (season == 0)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 1)
                {
                    if (txtStime.Value == DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() && txtEtime.Value == DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
                else if (season == 2)
                {
                    if (txtStime.Value == DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01" && txtEtime.Value == DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString())
                    {
                        lnkMonth.Style.Add(HtmlTextWriterStyle.Color, "blue");
                        lnkSeason.Style.Add(HtmlTextWriterStyle.Color, "red");
                        lnkYear.Style.Add(HtmlTextWriterStyle.Color, "blue");
                    }
                }
            }
            
        }
        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            getStartEndDate();
            DoReport();
        }
    }
}